package cn.rxyb.boot.rabbitmq.config;

import cn.rxyb.boot.rabbitmq.enums.QueueEnums;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author YJH
 * @version 1.0
 * @description
 * @date 2020/6/29 17:15
 */
@Configuration
@Slf4j
public class LogQueueImpl {

    /**
     * 获取队列
     *
     * @return
     */
    @Bean
    public Queue getQueue() {
        log.info("加载了队列" + QueueEnums.R_LOG_RECORD.getQueue());
        Map<String, Object> arguments = new HashMap<>(2);
        // 绑定该队列到私信交换机
        arguments.put("x-dead-letter-exchange", QueueEnums.R_DTL_RECORD1.getExchange());
        arguments.put("x-dead-letter-routing-key", QueueEnums.R_DTL_RECORD1.getRouting());
        // 设置过期时间
        arguments.put("x-message-ttl", 4000L);
        return new Queue(QueueEnums.R_LOG_RECORD.getQueue(), true, false, false, arguments);
    }


    /**
     * 策略的Exchange交换机实例
     *
     * @return
     */
    @Bean
    public DirectExchange getExchange() {
        return new DirectExchange(QueueEnums.R_LOG_RECORD.getExchange());
    }

    /**
     * 队列绑定
     *
     * @return
     */
    @Bean
    public Binding bindingExchange() {
        return BindingBuilder.bind(this.getQueue()).to(this.getExchange()).with(QueueEnums.R_LOG_RECORD.getRouting());
    }


    /**************************************************************************************************************/

    @Bean
    public DirectExchange dlxExchange() {
        return new DirectExchange(QueueEnums.R_DTL_RECORD1.getExchange());
    }

    @Bean
    public Queue dlxQueue() {
        return new Queue(QueueEnums.R_DTL_RECORD1.getQueue());
    }


    /**
     * 死信路由通过 DL_KEY 绑定键绑定到死信队列上.
     *
     * @return the binding
     */
    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder.bind(this.dlxQueue()).to(this.dlxExchange()).with(QueueEnums.R_DTL_RECORD1.getRouting());
    }
}
